//
// Created by QU on 24-6-27.
//

#include "logger.h"

namespace aquawius::json::logger::inline v1
{
    void Logger::log(const LOG_LEVELS level, const std::string& message)
    {
        std::string levelStr;
        switch (level)
        {
        case LOG_LEVELS::DEBUG: levelStr = "DEBUG";
            break;
        case LOG_LEVELS::INFO: levelStr = "INFO";
            break;
        case LOG_LEVELS::WARN: levelStr = "WARN";
            break;
        case LOG_LEVELS::ERROR: levelStr = "ERROR";
            break;
        case LOG_LEVELS::FATAL: levelStr = "FATAL";
            break;
        }

        const auto timeStr = getFormattedTimeString();

        std::cout << "[" << timeStr << "] [" << levelStr << "] " << message << std::endl;
    }

    std::string Logger::getFormattedTimeString()
    {
        const auto now = std::chrono::system_clock::now();
        const auto now_time_t = std::chrono::system_clock::to_time_t(now); // get yyyy-MM-dd hh:mm:ss
        const auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000; // get micro

        const std::tm now_tm = *std::localtime(&now_time_t);
        std::stringstream ss;
        // 使用 std::put_time 来格式化时间
        ss << std::put_time(&now_tm, "%Y-%m-%d %H:%M:%S"); // 格式化日期和时间
        ss << '.' << std::setfill('0') << std::setw(3) << now_ms.count(); // 格式化毫秒部分
        return ss.str();
    }
} // namespace aquawius::json::logger::inline v1


namespace aquawius::json::logger::v2
{
}
